package com.manager.login.ssm_student.controller;


import com.manager.common.result.Result;
import com.manager.common.result.ResultCodeEnum;
import com.manager.login.ssm_student.service.LoginUserService;
import com.manager.model.login.LoginForm;
import com.manager.model.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@CrossOrigin
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginUserService userservice;

    /**
     * wangliang
     * @param loginForm
     * @return
     */
    @PostMapping
    public Result login(@RequestBody LoginForm loginForm) {
//        获取前端传来的用户名密码
        String account = loginForm.getAccount();
        String password = loginForm.getPassword();
        Integer role = loginForm.getRole();
        User user = userservice.GetUserByAccount(account);
        User user1 = new User();
        if(user == null) {
//            System.out.println(user);
            return Result.fail(ResultCodeEnum.LOGIN_MOBLE_ERROR.getMessage());
        }
        else if(password == null ? user.getPassword() != null : !password.equals(user.getPassword())) {
//            System.out.println(password);
//            System.out.printf(user.getPassword());
            return Result.fail(ResultCodeEnum.PASSWORD_ERROR.getMessage());
        }
        else if (user.getRole() != role) {
            return Result.fail(ResultCodeEnum.LOGIN_ACL.getMessage());
        } else if (user.getStatus() == 0) {
            return Result.fail(ResultCodeEnum.LOGIN_DISABLED_ERROR.getMessage());
        } else{
            if(role==1){
                user1.setAccount(user.getAccount());
                user1.setName(user.getName());
                user1.setId(user.getId());
                return Result.ok(user1);
            }
            else{
                user1.setAccount(user.getAccount());
                user1.setName(user.getName());
                return Result.ok(user1);
            }

        }
    }
}
